"""版本一GenericAPIView"""

# from meiduo_admin.serializer.order_serializer import *
# from meiduo_admin.pages import MyPage
# from rest_framework.generics import GenericAPIView
# from rest_framework.response import Response
# from rest_framework.generics import UpdateAPIView
#
#
# # GET orders/
# class OrderView(GenericAPIView):
#     serializer_class = OrderSerializer
#     queryset = OrderInfo.objects.all()
#     pagination_class = MyPage
#
#     """获取所有订单"""
#     def get(self, request):
#         orders = self.get_queryset()
#         page = self.paginate_queryset(orders)
#         if page:
#             page_serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(page_serializer.data)
#         serializer = self.get_serializer(orders, many=True)
#         return Response(serializer.data)
#
#     def get_queryset(self):
#         # 根据订单id过滤
#         keyword = self.request.query_params.get('keyword')
#         if keyword:
#             return self.queryset.filter(order_id__contains=keyword)
#         return self.queryset.all()
#
#
# # GET orders/(?P<pk>\d+)/
# class OrderParticularsView(UpdateAPIView):
#     serializer_class = OrdersSerializer
#     queryset = OrderInfo.objects.all()
#
#     """获取单一数据"""
#     def get(self, request, pk):
#         order_obj = self.get_object()
#         serializer = self.get_serializer(order_obj)
#         return Response(serializer.data)
#
#
#

"""版本二"""

from meiduo_admin.serializer.order_serializer import *
from meiduo_admin.pages import MyPage
from rest_framework.generics import ListAPIView, RetrieveAPIView, UpdateAPIView


# GET orders/
class OrderView(ListAPIView):
    """获取所有订单"""
    serializer_class = OrderSerializer
    queryset = OrderInfo.objects.all()
    pagination_class = MyPage

    def get_queryset(self):
        # 根据订单id过滤
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return self.queryset.filter(order_id__contains=keyword)
        return self.queryset.all()


# GET orders/(?P<pk>\d+)/
class OrderParticularsView(RetrieveAPIView, UpdateAPIView):
    """获取单一数据"""
    serializer_class = OrdersSerializer
    queryset = OrderInfo.objects.all()

